<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="联系人" prop="linkName">
						<el-input v-model="form.linkName" placeholder="联系人" clearable maxlength="80"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="关联客户" prop="customerId">
						<sc-select v-model="form.customerId" :apiObj="$API.crm.crmCustomer.getMyCustomerListForSelect" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="性别" prop="sex">
						<el-radio-group v-model="form.sex">
							<el-radio-button label="1">男</el-radio-button>
							<el-radio-button label="2">女</el-radio-button>
							<el-radio-button label="0">保密</el-radio-button>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="出生日期" prop="birthday">
						<el-date-picker :disabled-date="publishDateAfter" v-model="form.birthday" placeholder="出生日期" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="手机号" prop="mobile">
						<el-input v-model="form.mobile" placeholder="手机号" clearable maxlength="50"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="电子邮件" prop="email">
						<el-input v-model="form.email" placeholder="电子邮件" clearable maxlength="50"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="部门" prop="deptName">
						<el-input v-model="form.deptName" placeholder="部门" clearable maxlength="40"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="职务" prop="post">
						<el-input v-model="form.post" placeholder="职务" clearable maxlength="40"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="决策人" prop="decisionMaker">
						<el-radio-group v-model="form.decisionMaker">
							<el-radio-button label="1">是</el-radio-button>
							<el-radio-button label="0">否</el-radio-button>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="上级领导" prop="leadName">
						<el-input v-model="form.leadName" placeholder="上级领导" clearable maxlength="50"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="电话" prop="tel">
						<el-input v-model="form.tel" placeholder="电话" clearable maxlength="50"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="传真" prop="fax">
						<el-input v-model="form.fax" placeholder="传真" clearable maxlength="50"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="爱好" prop="hobby">
						<el-input v-model="form.hobby" placeholder="爱好" clearable maxlength="50"></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="通讯地址" prop="address">
						<el-input v-model="form.address" placeholder="通讯地址" clearable maxlength="50"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="备注说明" prop="remark">
				<el-input v-model="form.remark" placeholder="备注说明" clearable maxlength="50" type="textarea"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import {verifyEmail, verifyPhone} from "@/utils/verificate";

export default {
	emits: ['success', 'closed'],
	components: {},
	data() {
		return {
			visible: false,
			isSaveing: false,
			mode: "add",
			btName: "保存",
			titleMap: {
				add: '创建联系人',
				edit: '编辑联系人',
				show: '查看'
			},
			fileList: [],
			form: {
				linkManId: '',
				linkName: '',
				customerId: '',
				sex: '1',
				email: '',
				mobile: '',
				deptName: "",
				post: "",
				birthday: '',
				decisionMaker: '1',
				leadName: '',
				tel: '',
				fax: '',
				hobby: '',
				address: '',
				remark: ''
			},
			rules: {
				linkName: [
					{required: true, message: '联系人不能为空', trigger: 'blur'}
				],
				mobile: [
					{validator: verifyPhone, trigger: 'blur'}
				],
				email: [
					{validator: verifyEmail, trigger: 'blur'}
				],
			}
		}
	},
	mounted() {
	},
	methods: {
		publishDateAfter(time) {
			return time.getTime() > Date.now()
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.crm.crmLinkMan.insertCrmLinkMan.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.crm.crmLinkMan.updateCrmLinkMan.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.linkManId = data.linkManId
			this.form.linkName = data.linkName
			this.form.customerId = data.customerId
			this.form.sex = data.sex
			this.form.email = data.email
			this.form.mobile = data.mobile
			this.form.deptName = data.deptName
			this.form.post = data.post
			this.form.birthday = data.birthday
			this.form.decisionMaker = data.decisionMaker
			this.form.leadName = data.leadName
			this.form.tel = data.tel
			this.form.fax = data.fax
			this.form.hobby = data.hobby
			this.form.address = data.address
			this.form.remark = data.remark
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
